//
//  STDSWihteBalanceEffect.h
//  STDSRenderSDKIOS
//
//  Created by 陈学明 on 2023/4/3.
//

#ifndef STDSWihteBalanceEffect_h
#define STDSWihteBalanceEffect_h

#include "STDSBaseEffect.hpp"

class STDSWihteBalanceEffect: public STDSBaseEffect {
public:
    STDSWihteBalanceEffect() {
        mEffectType = STDSEffectTypeWhiteBalance;
    }
    ~STDSWihteBalanceEffect() {
//        printf("dealloc--STDSMosaicEffect\n");
    }
    
    void initGL() override {
        STDSBaseEffect::initGL();
    }
    
    void setFloatArguments(const char *name, float *floatValue, int n) override {
        if (strcmp("temperature", name) == 0) {
            mTemperature = *floatValue;
        } else if (strcmp("tint", name) == 0) {
            mTint = *floatValue;
        }
    }
    
public:
    shared_ptr<STDSShaderProgram> getEffectProgram() {
        if (mWhiteBalanceProgram == nullptr) {
            mWhiteBalanceProgram = mContext->getProgram(kShaderSRCWhiteBalance);
        }
        return mWhiteBalanceProgram;
    }
    
    void draw() override {
        auto program = getEffectProgram();
        if (program == nullptr) {
            return;
        }
        if (mTexture == nullptr) {
            return;
        }
        
        program->use();
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, mTexture->getId());
        program->setUniform(program->getUniform("inputImageTexture"), 0);
        program->setUniform(program->getUniform("temperature"), mTemperature);
        program->setUniform(program->getUniform("tint"), mTint);
        
        mAtbRect.setPosVex(program->getAttrib("aPos"));
        mAtbRect.setPosTex(program->getAttrib("aTexCoord"));
        mAtbRect.draw();
        program->unuse();
    }

private:
    float mTemperature{0.0};
    float mTint{0.0};
    shared_ptr<STDSShaderProgram> mWhiteBalanceProgram{nullptr};
};

#endif /* STDSWihteBalanceEffect_h */
